#include "Chunk.h"
#include "Minecraft/File Helpers/FileLoader.h"
#include <memory.h>
#include <iostream>
using namespace std;

static int broken = 0;

Chunk::Chunk()
{

}
void Chunk::fillBlockId(UBYTE* blockStream)
{
	memcpy(BlockId, blockStream, 32768);
}

void Chunk::fillAdditionalData(UBYTE* dataStream)
{
	memcpy(AdditionalData, dataStream, 16384);
}

void Chunk::fillBlocklightData(UBYTE* blocklightStream)
{
	memcpy(BlockLight, blocklightStream, 16384);
}

void Chunk::fillSkylightData(UBYTE* skyStream)
{
	memcpy(SkyLight, skyStream, 16384);
}

void Chunk::fillHeightMap(UBYTE* heightStream)
{
	memcpy(HeightMap, heightStream, 256);
}
BLOCK_TYPE Chunk::getBlockType(int x, int y, int z)
{
	if( x < 1 || x > 16)
		return BAD_X_PARAM;
	if( y < 1 || y > 128)
		return BAD_Y_PARAM;
	if( z < 1 || z > 16)
		return BAD_Z_PARAM;

	return (BLOCK_TYPE)BlockId[y + ((z * 128) + (x * 128 * 16))];
}

void Chunk::LoadFromNBT(const char *_szFileName)
{
	MinecraftFile mcFile;
	bool success = mcFile.Load(_szFileName);
	if(success == false)
	{
		char buffer[64];
		sprintf(buffer, _szFileName);
		cout << ++broken << ' ' << buffer << '\n';
		return;
	}
	mcFile.DecodeIntoChunk(this);
}

void Chunk::Zero()
{
	xPos = 0;
	zPos = 0;
	memset(BlockId, 0, 32768);
	memset(AdditionalData, 0, 16384);
	memset(SkyLight, 0, 16384);
	memset(BlockLight, 0, 16384);
	memset(HeightMap, 0, 256);
}